package udpSocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class layerOneServer {

	private DatagramSocket serverSocket;
	private static InetAddress ipAddress;
	private static int port;

	public layerOneServer() throws Exception{
		serverSocket = new DatagramSocket(9876);
	}

	public void serverSend(String ack) throws Exception{
		byte[] sendData = new byte[1024];
		String acknowledge = ack;
		sendData = acknowledge.getBytes();
		DatagramPacket sendPacket =
				new DatagramPacket(sendData, sendData.length, this.getIpAddress(), this.getPort());
		serverSocket.send(sendPacket);
	}

	public String serverReceive() throws Exception{
		byte[] receiveData = new byte[1024];
		DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
		serverSocket.receive(receivePacket);
		setIP(receivePacket.getAddress());
		setPort(receivePacket.getPort());

		return new String(receivePacket.getData());

	}

	public static void setIP(InetAddress ip){
		ipAddress = ip;
	}

	public static void setPort(int portNumber){
		port = portNumber;
	}

	public InetAddress getIpAddress(){
		return ipAddress;
	}

	public int getPort(){
		return port;
	}

}
